import LayerManager from "./Libs/LayerManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default abstract class Root extends cc.Component {
    //根节点
    @property(cc.Node)
    root:cc.Node = null

    onLoad () {
        //游戏帧率
        cc.game.setFrameRate(30);

    }

    protected async start(){

        //设置ui根节点
        LayerManager.setRoot(this.root)

        this.registerGameEvent();

        // 创建持久根节点
        let persistRootNode = new cc.Node("PersistRootNode");
        cc.game.addPersistRootNode(persistRootNode);

        // 创建音频模块
        //persistRootNode.addComponent(AudioManager);

        this.run();
    }

    abstract run()
    


    /** 注册game事件 */
    private registerGameEvent() {
        if(cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.onShow(this.onGameShow.bind(this));
            wx.onHide(this.onGameHide.bind(this));
        }else {
            cc.game.on(cc.game.EVENT_SHOW, this.onGameShow, this);
            cc.game.on(cc.game.EVENT_HIDE, this.onGameHide, this);
        }
    }

    private onGameShow(param: any) {
        //Logger.trace("Game.EVENT_SHOW");
        cc.director.resume()
    }

    private onGameHide() {
       // Logger.trace("Game.EVENT_HIDE");
        cc.director.pause();
    }
}
